package co.chuangdian.oflauncher.setting;

import uc.CircleGallery;
import co.chuangdian.oflauncher.OFActivity;
import co.chuangdian.oflauncher.R;
import java.util.ArrayList;
import java.util.List;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.annotation.SuppressLint;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AbsListView.LayoutParams;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import cn.onekit.ASSET;
import cn.onekit.CONFIG;
import cn.onekit.JSON;
import cn.onekit.MESSAGE;
import cn.onekit.android.ACTIVITY;

public class GesturesActivity extends OFActivity {
	CircleGallery page_gallery;
	JSONArray allGestures = new JSONArray();
	JSONArray gestures = new JSONArray();
	// WheelView picker;
	int pageIndex = 0;
	JSONObject setting, gesturesSetting;
	private ListView listView;
	private TextView title_name;
	private ImageView title_icon;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.setting_gestures);
		allGestures = ASSET.loadJSONs("gestures.json");
		setting = JSON.parse((String) CONFIG.get("setting"));
		gesturesSetting = setting.optJSONObject("gestures");
		listView = (ListView) findViewById(R.id.litview);
		title_name = (TextView) findViewById(R.id.title_name);
		title_icon = (ImageView) findViewById(R.id.title_icon);
		// picker = (WheelView) this.findViewById(R.id.picker);
		/*
		 * picker.setOnItemSelectedListener(new
		 * TosAdapterView.OnItemSelectedListener() {
		 * 
		 * @Override public void onItemSelected(TosAdapterView<?> parent, View
		 * view, int position, long id) { switch (pageIndex) { case 0: try {
		 * gesturesSetting.put("folders_widgets", position + 1);
		 * CONFIG.set("setting", setting);
		 * MESSAGE.send("[setting]folders_widgets",null, null); } catch
		 * (JSONException e1) { e1.printStackTrace(); } break; case 1: try {
		 * gesturesSetting.put("sidebars", position + 1); CONFIG.set("setting",
		 * setting); MESSAGE.send("[setting]sidebars",null, null); } catch
		 * (JSONException e) { e.printStackTrace(); } break; default: break; } }
		 * 
		 * @Override public void onNothingSelected(TosAdapterView<?> parent) {
		 * 
		 * }
		 * 
		 * });
		 */
		title_icon.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (listVisib()) {
					title_icon.setImageResource(R.drawable.btn_arrow_up);
					listView.setVisibility(View.GONE);

				} else {
					title_icon.setImageResource(R.drawable.btn_arrow_down);
					listView.setVisibility(View.VISIBLE);
				}
			}
		});
		listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				switch (pageIndex) {
				case 0:
					try {
						gesturesSetting.put("folders_widgets", position + 1);
						CONFIG.set("setting", setting);
						MESSAGE.send("[setting]folders_widgets", null, null);
						listView.setVisibility(View.GONE);
						title_icon.setImageResource(R.drawable.btn_arrow_up);

						String resName = gestures.optString(gesturesSetting.optInt("folders_widgets") - 1);
						int resID = GesturesActivity.this.getStringID(resName);
						String title = GesturesActivity.this.getString(resID);
						title_name.setText(title);

					} catch (JSONException e1) {
						e1.printStackTrace();
					}
					break;
				case 1:
					try {
						gesturesSetting.put("sidebars", position + 1);
						CONFIG.set("setting", setting);
						MESSAGE.send("[setting]sidebars", null, null);
						listView.setVisibility(View.GONE);
						title_icon.setImageResource(R.drawable.btn_arrow_up);

						String sidresName = gestures.optString(gesturesSetting.optInt("sidebars") - 1);
						int sidresID = GesturesActivity.this.getStringID(sidresName);
						String sidtitle = GesturesActivity.this.getString(sidresID);
						title_name.setText(sidtitle);

					} catch (JSONException e) {
						e.printStackTrace();
					}
					break;
				default:
					break;
				}
			}
		});

		listView.setAdapter(new BaseAdapter() {

			@Override
			public int getCount() {
				return gestures.length();
			}

			@Override
			public Object getItem(int position) {
				return null;
			}

			@Override
			public long getItemId(int position) {
				return 0;
			}

			@SuppressLint("ViewHolder")
			@SuppressWarnings("deprecation")
			@Override
			public View getView(int position, View convertView, ViewGroup parent) {
				View view = View.inflate(context, R.layout.gesture_item, null);
				TextView tv_gesture = (TextView) view.findViewById(R.id.tv_gesture);
				String resName = gestures.optString(position);
				int resID = GesturesActivity.this.getStringID(resName);
				String title = GesturesActivity.this.getString(resID);
				tv_gesture.setText(title);
				return view;
			}
		});
		page_gallery = (CircleGallery) this.findViewById(R.id.page_gallery);
		List<String> titles = new ArrayList<String>();
		List<Bitmap> images = new ArrayList<Bitmap>();
		for (int i = 0; i < allGestures.length(); i++) {
			JSONObject gesture = allGestures.optJSONObject(i);
			titles.add(getString(getStringID(gesture.optString("nameID"))));
			images.add(ASSET.loadImage(gesture.optString("image"), true));
		}
		page_gallery.setData(titles.toArray(new String[titles.size()]), images.toArray(new Bitmap[images.size()]));
		page_gallery.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
				// picker.setVisibility(View.VISIBLE);
				pageIndex = arg2;
				gestures = allGestures.optJSONObject(pageIndex).optJSONArray("options");
				((BaseAdapter) listView.getAdapter()).notifyDataSetChanged();

				switch (pageIndex) {
				case 0:
					String resName = gestures.optString(gesturesSetting.optInt("folders_widgets") - 1);
					int resID = GesturesActivity.this.getStringID(resName);
					String title = GesturesActivity.this.getString(resID);
					title_name.setText(title);
					// picker.setSelection();
					break;
				case 1:
					String sidresName = gestures.optString(gesturesSetting.optInt("sidebars") - 1);
					int sidresID = GesturesActivity.this.getStringID(sidresName);
					String sidtitle = GesturesActivity.this.getString(sidresID);
					title_name.setText(sidtitle);
					// picker.setSelection(gesturesSetting.optInt("sidebars") -
					// 1);
					break;
				default:
					break;
				}
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				// picker.setVisibility(View.INVISIBLE);
			}
		});
		//
	}

	/**
	 * 判断listview是否可见
	 * 
	 * @return
	 */
	public Boolean listVisib() {
		if (listView.getVisibility() == View.GONE) {
			return false;
		}
		return true;
	}
}
